/**
 * Created by Tuffy on 16/1/18.
 */
'use strict';

angular.module('starter.controllers')

.controller('HotelActivationCodeListCtrl', ['$rootScope','$scope', '$ionicActionSheet', '$state','$timeout', 'HotelActivationCodeService', function($rootScope,$scope, $ionicActionSheet, $state,$timeout, HotelActivationCodeService) {

    $scope.title = '酒店编码查询';

    /**
     * contact
     */
    $scope.activationCode ={
        search: {
            value:'',
            doAct:function(){
                $scope.activationCode.page.pageNum = 1;
                if(this.timer){
                    $timeout.cancel(this.timer);
                }
                this.timer = $timeout(function(){
                    $scope.activationCode.data = [];
                    $scope.activationCode.loadData();
                },500);
            }
        },
        page: {
            pageNum:1,
            pageSize:10,
            total:200
        },
        loaded:false,
        data:[/*{
            hotelName:"泰州兴化市会友旅馆",
            hotelAddr:"泰州市",
            hotelCode:"123456",
            hotelStatus:"未激活"

        }*/],
        loadData:function(okFunc){
            var _this = this,
                params = {
                    userId :$rootScope.localStorageObj.userId,
                    para:_this.search.value,
                    pageNum:_this.page.pageNum,
                    pageSize:_this.page.pageSize
                };
            _this.loaded = false;
            for(var key in _this.search.filter){
                params[key] = _this.search.filter[key];
            }
            HotelActivationCodeService.findList(params)
                .success(function(res){
                    if(res.success){
                        if(typeof (okFunc) == 'function'){
                            okFunc();
                        }
                        _this.data = _this.data.concat(res.attribute.rows);
                        _this.page.total = res.attribute.totle;
                    }else{
                        _this.page.total = 0;
                        console.log(res.errorMsg);
                    }
                }).error(function(error){
                    _this.page.total = 0;
                    console.log(error);
                }).finally(function(){
                    _this.loaded = true;
                    $scope.$broadcast('scroll.refreshComplete');
                    $scope.$broadcast('scroll.infiniteScrollComplete');
                });
        },
        moreDataCanBeLoaded:function(){
            return this.page.total > this.page.pageNum*this.page.pageSize;
        },
        loadMore:function(){
            this.page.pageNum++;
            this.loadData();
        },
        refresh:function(){
            var _this = this;
            _this.page.pageNum = 1;
            _this.loadData(function(){
                _this.data = [];
            });
        }
    } ;
    $scope.activationCode.loadData();

    // 获取详细
    $scope.go2Detail = function(detail) {
        $rootScope.activationCodeDetail = detail;
        $state.go('app.hotel-activation-code-detail');
    };
}])

// 激活码详情
.controller('HotelActivationCodeDetailCtrl', ['$rootScope', '$scope', function($rootScope, $scope) {

}])

.service('HotelActivationCodeService',['CrmHttp','$rootScope',function(CrmHttp, $rootScope){
    var url = '/pmsController/';
    return {
        /**
         * 用户通讯录
         * @param params
         * @returns {*}
         */
        findList: function (params) {
            var act = 'searchPmsHotels';
            return CrmHttp.post(url + act, params);
        },
    };
}]);